package com.xin.net
{
import com.xin.utils.URLUtils;

import flash.display.Bitmap;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.media.Sound;
import flash.utils.Dictionary;
/**
 * 资源管理
 * @author daichangxin
 */
public class ResManager extends EventDispatcher
{
	private static var _instance:ResManager;
	
	private var _urlList:Array = [];
	private var _loaderDic:Dictionary = new Dictionary();
	
	public function ResManager()
	{
		if (_instance == null)
		{
			_instance = this;
		}
	}
	
	private function getLoader(url:String):LoaderBase
	{
		var urlUtils:URLUtils = new URLUtils(url);
		switch(urlUtils.fileType){
			case "swf":
				return new SWFLoader(url);
				
			case "png":
			case "jpg":
			case "jpg":
				return new ImageLoader(url);
				
			case "mp3":
				return new MP3Loader(url);
				
			case "xml":
				return new XMLLoader(url);
		}
		
		return null;
	}
	
	private function addListeners(loader:LoaderBase):void
	{
		loader.addEventListener(ErrorEvent.ERROR, loader_error);
		loader.addEventListener(Event.COMPLETE, loader_complete);
	}
	
	private function removeListeners(loader:LoaderBase):void
	{
		loader.removeEventListener(ErrorEvent.ERROR, loader_error);
		loader.removeEventListener(Event.COMPLETE, loader_complete);
	}
	
	/**
	 * 单个loader加载完成
	 */
	private function loader_complete(e:Event):void
	{
		var loader:LoaderBase = e.currentTarget as LoaderBase;
		removeListeners(loader);
		
		//记录加载的数据
		_loaderDic[loader.url] = loader;
		
		//加载下一个
		load();
	}
	
	/**
	 * 单个loader加载失败
	 */
	private function loader_error(e:ErrorEvent):void
	{
		trace(e.text);
		removeListeners(e.currentTarget as LoaderBase);
		
		load();
	}
	
	/**
	 * 加载资源
	 */
	public function load():void
	{
		var url:String = _urlList.shift();
		if (url == null)
		{
			dispatchEvent(new Event(Event.COMPLETE));
		}
		else
		{
			var loader:LoaderBase = getLoader(url);
			addListeners(loader);
			loader.load();
		}
	}
	
	/**
	 * 添加资源地址
	 */
	public function addURL(url:String):void
	{
		_urlList.push(url);
	}
	
	/**
	 * 获取位图
	 */
	public function getBitmap(url:String):Bitmap
	{
		var loader:LoaderBase = _loaderDic[url];
		if (loader == null)	return null;
		
		var imageLoader:ImageLoader = loader as ImageLoader;
		if (imageLoader == null) return null;
			
		return imageLoader.getBitmap();
	}
	
	/**
	 * 获取声音
	 */
	public function getSound(url:String):Sound
	{
		var loader:LoaderBase = _loaderDic[url];
		if (loader == null) return null;
		
		var mp3Loader:MP3Loader = loader as MP3Loader;
		if (mp3Loader == null) return null;
		
		return mp3Loader.getSound();
	}
	
	/**
	 * 获取XML文件
	 */
	public function getXML(url:String):XML
	{
		var loader:LoaderBase = _loaderDic[url];
		if (loader == null) return null;
		
		var xmlLoader:XMLLoader = loader as XMLLoader;
		if (xmlLoader == null) return null;
		
		return xmlLoader.getXML();
	}
	
	public static function getInstance():ResManager
	{
		if (_instance == null)
		{
			_instance = new ResManager();
		}
		return _instance;
	}
}
}
